<?php

namespace apiend\modules\dynamic\controllers;

use apiend\modules\dynamic\models\DynamicVideoForm;
use Yii;
use apiend\components\Controller;
use apiend\modules\dynamic\models\DynamicForm;
use apiend\modules\dynamic\models\DynamicCommentForm;

/**
 * 动态发布相关接口
 * Default controller for the `dynamic` module
 */
class PublishController extends Controller {

    /**
     * @inheritdoc
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            '*' => ['POST'],
        ];
    }

    /**
     * 发布动态
     * @return string
     */
    public function actionCreate() {
        $model = new DynamicForm();
        $post = Yii::$app->request->post();
        if (!$model->load($post, '')) {
            parent::warning('动态更新时提交的数据未加载成功');
        }
        $model->user_id = $this->token->user_id;
        $model->device_id = $this->token->device_id;
        return $model->save();
    }

    /**
     * 发表评论
     * @return string
     */
    public function actionComment() {
        $model = new DynamicCommentForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::warning('评论请求失败，请检查参数的有效性');
        }
        $model->user_id = $this->token->user_id;
        $model->device_id = $this->token->device_id;
        return $model->save();
    }

    /**
     * 发表视频
     */
    public function actionVideo() {
        $model = new DynamicVideoForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::warning('视频动态更新时提交的数据未加载成功');
        }
        $model->user_id = $this->token->user_id;
        $model->device_id = $this->token->device_id;
        return $model->save();
    }

}
